package com.example.forestmanagement.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.forestmanagement.R;
import com.example.forestmanagement.model.MarkData;
import java.util.List;

public class MarkListAdapter extends RecyclerView.Adapter<MarkListAdapter.ViewHolder> {
    private List<MarkData> markList;
    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(MarkData mark);
    }

    public MarkListAdapter(List<MarkData> markList, OnItemClickListener listener) {
        this.markList = markList;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_mark, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        MarkData mark = markList.get(position);
        holder.tvMarkName.setText(mark.getLabelName());
        holder.tvMarkDesc.setText(mark.getLabelRemark());
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(mark);
            }
        });
    }

    @Override
    public int getItemCount() {
        return markList != null ? markList.size() : 0;
    }

    public void updateData(List<MarkData> newData) {
        this.markList = newData;
        notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvMarkName;
        TextView tvMarkDesc;

        ViewHolder(View itemView) {
            super(itemView);
            tvMarkName = itemView.findViewById(R.id.tv_mark_name);
            tvMarkDesc = itemView.findViewById(R.id.tv_mark_desc);
        }
    }
} 